#include "StdAfx.h"
#include "qihmessagewindow.h"

QihMessageWindow::QihMessageWindow(QihMessageWndClass& wndClass)
{
	_handle = ::CreateWindow(wndClass.name().toStdWString().c_str(), 0, 0, 0, 0, 0, 0, 
		HWND_MESSAGE, 0, wndClass.instance(), 0);
	assert(_handle != NULL);
}

QihMessageWindow::~QihMessageWindow()
{
	::DestroyWindow(_handle);
}

void QihMessageWindow::create()
{

}

void QihMessageWindow::destroy()
{

}

void QihMessageWindow::registerCallback( quint32 msgid, QihMessageCallback* callback )
{
	registerCallback(msgid, msgid, callback);
}

void QihMessageWindow::registerCallback( quint32 firstMsgId, quint32 lastMsgId, QihMessageCallback* callback )
{
	CallbackRegInfo* cri = new CallbackRegInfo;
	cri->firstMsgId = firstMsgId;
	cri->lastMsgId = lastMsgId;
	cri->callback = callback;
	_callbacks.append(cri);
}

void QihMessageWindow::unregiserCallback( QihMessageCallback* callback )
{
	//TODO: QihMessageWindow::unregiserCallback
}

void QihMessageWindow::unregisterAllCallbacks()
{
	//TODO: QihMessageWindow::unregisterAllCallbacks()
}

LRESULT QihMessageWindow::dispatchMessage( UINT message, WPARAM wparam, LPARAM lparam )
{
	foreach(CallbackRegInfo* cri, _callbacks)
	{
		if(message >= cri->firstMsgId && message <= cri->lastMsgId )
			return cri->callback->handleMessage(_handle, message, wparam, lparam);
	}
	return ::DefWindowProc(_handle, message, wparam, lparam);
}

